<?php
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'is_agent'=>'1');
	if($_RQ['mid']){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	//创建时间
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
		$return['params']['createtime']=$time_range;
	}
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'orderby'=>'agenttime desc',
		
	);
	$list=$this->get_list('member',$parameter,$total);
	if($list){
		foreach($list as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			$item['agent']=cfc('core')->member_show($item['agent_id'],'',array('nickname'=>'总店'));
			$item['from_os']=pdo_getcolumn('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['mid']),'from_os');
			$item['from_os']=$item['from_os']?get_ostitle()[$item['from_os']]:'微信';
			$item['url']=custom_url('jy_mfee/myshop/main/mid='.$item['mid'].'&fromuser='.$item['mid'],true);
			$this->model('member')->min_agent_level($item['mid']);
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'export'=>'export',
		'list'=>array('data'=>'list','del_one'=>false,'delall'=>false,'tip'=>'<red>[温馨提示]：关于匿名用户的解释：正常认证服务号，没有关闭获取粉丝详细的情况下，是不会存在匿名用户的；可能小程序出现匿名用户的情况居多：原因是：小程序官方不允许首次访问弹窗获取粉丝详细。但是为了上下级的准确性，在访问小程序首页的时候就产生了下级关系！这时候就会出现匿名用户的情况。但是只要这个匿名用户访问了购物车，商品，会员中心等任何页面。就会弹窗获取粉丝信息。这时候匿名用户就会变成带昵称头像的会员了！</red>','key'=>'mid','edit'=>false,'del_message'=>'确定删除分商商吗？会同时解除其下所有上下级关系哦！','list'=>array(
			array('name'=>'mid','title'=>'分销商MID','type'=>'string'),
			array('name'=>'member','title'=>'分销商','type'=>'member'),
			array('name'=>'agent','title'=>'推荐人','type'=>'member'),
			array('name'=>'url','title'=>'推广二维码','type'=>'ewm'),
			array('name'=>'from_os','title'=>'来源','type'=>'string'),
			array('name'=>'level_id','title'=>'分销商等级','type'=>'select','options'=>'levels'),
			array('name'=>'agenttime','title'=>'成为分销商时间','type'=>'time'),
			array('name'=>'stop','title'=>'分销商状态','type'=>'radio','options'=>'agent_status'),
			array('name'=>'member_num','title'=>'下线数','type'=>'string'),
			array('name'=>'agent_num','title'=>'下级分销商数','type'=>'string'),
			array('name'=>'order_num','title'=>'推广订单数','type'=>'string'),
			array('name'=>'order_price','title'=>'推广订单总金额','type'=>'string'),
		),'action'=>array(
			array('type'=>'modal','op'=>'detail','title'=>'分销商信息'),
			array('type'=>'href','op'=>'member','title'=>'他的下线'),
		)),
		'search'=>array(
			array('type'=>'choose','width'=>15,'action'=>'core/member.get_member/','name'=>'mid','title'=>'选择会员'),
			array('name'=>'createtime','width'=>15,'title'=>'加入时间','type'=>'time_range'),
		),
	);
	$return['htmls']=$htmls;
	$return['agent_status']=$this->agent_status;
	$return['levels']=$this->getall('level',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['levels']['0']=array('id'=>'0','title'=>'默认等级');
	exi($return);
}
if($_RQ['op']=='export'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'is_agent'=>'1');
	if($_RQ['mid']){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	//创建时间
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
	}
	$list=pdo_getall('jy_mfee_member',$params);
	$agent_status=$this->agent_status;
	if($list){
		foreach($list as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			$item['name']=$item['member']['nickname'];
			$item['mobile']=$item['member']['mobile'];
			$item['agent']=cfc('core')->member_show($item['agent_id'],'',array('nickname'=>'总店'));
			$item['agentname']=$item['agent']['nickname'];
			$item['from_os']=pdo_getcolumn('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['mid']),'from_os');
			$item['from_os']=$item['from_os']?get_ostitle()[$item['from_os']]:'微信';
			$item['level_title']=$this->getcolumn('level',array('id'=>$item['level_id'],'uniacid'=>$_SESSION['uniacid']),'title');
			$item['level_title']=$item['level_title']?:'默认等级';
			$item['stop']=$agent_status[$item['stop']];
			$item['agenttime']=date('Y-m-d H:m:s',$item['agenttime']);
		}
	}
	$header=array(
		'mid'=>'分销商MID',
		'name'=>'分销商名称',
		'mobile'=>'分销商电话',
		'agentname'=>'推荐人',
		'from_os'=>'来源',
		'level_title'=>'分销商等级',
		'agenttime'=>'成为分销商时间',
		'stop'=>'分销商状态',
		'member_num'=>'下线数',
		'agent_num'=>'下级分销商数',
		'order_num'=>'推广订单总金额',
		'order_price'=>'推广订单总金额',
	);
	cfc('tools')->export2excel($header,$list,'分销商');
}
if($_RQ['op']=='export2'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'is_agent'=>intval($_RQ['mid']));
	//创建时间
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
	}
	$list=pdo_getall('jy_mfee_member',$params);
	$agent_status=$this->agent_status;
	if($list){
		foreach($list as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			$item['name']=$item['member']['nickname'];
			$item['mobile']=$item['member']['mobile'];
			$item['agent']=cfc('core')->member_show($item['agent_id'],'',array('nickname'=>'总店'));
			$item['agentname']=$item['agent']['nickname'];
			$item['from_os']=pdo_getcolumn('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['mid']),'from_os');
			$item['from_os']=$item['from_os']?get_ostitle()[$item['from_os']]:'微信';
			$item['level_title']=$this->getcolumn('level',array('id'=>$item['level_id'],'uniacid'=>$_SESSION['uniacid']),'title');
			$item['level_title']=$item['level_title']?:'默认等级';
			$item['stop']=$agent_status[$item['stop']];
			$item['createtime']=date('Y-m-d H:m:s',$item['createtime']);
		}
	}
	$header=array(
		'mid'=>'分销商MID',
		'name'=>'分销商名称',
		'mobile'=>'分销商电话',
		'agentname'=>'推荐人',
		'from_os'=>'来源',
		'level_title'=>'分销商等级',
		'createtime'=>'创建时间',
		'stop'=>'分销商状态',
		'member_num'=>'下线数',
		'agent_num'=>'下级分销商数',
		'order_num'=>'推广订单总金额',
		'order_price'=>'推广订单总金额',
	);
	cfc('tools')->export2excel($header,$list,'分销商');
}
if($_RQ['op']=='member'){
	
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid'],'agent_id'=>intval($_RQ['mid']));
	$return['params']['mid']=$_RQ['mid'];
	//是否分销商
	if(isset($_RQ['is_agent'])){
		$params['is_agent']=intval($_RQ['is_agent']);
		$return['params']['is_agent']=$_RQ['is_agent'];
	}
	//创建时间
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
		$return['params']['createtime']=$time_range;
	}
	$psize=20;
	$pindex=intval($_RQ['page'])?:1;
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'orderby'=>'createtime desc',
	);
	$list=$this->get_list('member',$parameter,$total);
	if($list){
		foreach($list as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			//$item['agent']=cfc('core')->member_show($item['agent_id'],'',array('nickname'=>'总店'));
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'export'=>'export2',
		'list'=>array('data'=>'list','key'=>'mid','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'member','title'=>'分销商','type'=>'member'),
			//array('name'=>'agent','title'=>'推荐人','type'=>'member'),
			array('name'=>'level_id','title'=>'分销商等级','type'=>'select','options'=>'levels'),
			array('name'=>'agenttime','title'=>'成为分销商时间','type'=>'time'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
			array('name'=>'stop','title'=>'分销商状态','type'=>'radio','options'=>'agent_status'),
			array('name'=>'member_num','title'=>'下线数','type'=>'string'),
			array('name'=>'agent_num','title'=>'下级分销商数','type'=>'string'),
			array('name'=>'order_num','title'=>'推广订单数','type'=>'string'),
			array('name'=>'order_price','title'=>'推广订单总金额','type'=>'string'),
		),'action'=>array(
			array('type'=>'modal','op'=>'detail','title'=>'分销商信息'),
			array('type'=>'href','op'=>'member','title'=>'他的下线'),
		)),
		'search'=>array(
			array('type'=>'choose','width'=>15,'action'=>'core/member.get_member/','name'=>'mid','title'=>'选择上级'),
			array('name'=>'createtime','width'=>15,'title'=>'加入时间','type'=>'time_range'),
		),
	);
	$return['htmls']=$htmls;
	$return['agent_status']=$this->agent_status;
	$return['levels']=$this->getall('level',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['levels']['0']=array('id'=>'0','title'=>'默认等级');
	exi($return);
}
if($_RQ['op']=='detail'){
	$mid=intval($_RQ['mid']);
	$item=pdo_get('jy_mfee_member',array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
	$item['member']=cfc('core')->member_show($item['mid']);
	$item['member']['avatar']=$item['member']['avatar']?:'../core/resource/images/gw-user.png';
	$item['realname']=$item['realname']?:$item['member']['realname'];
	$item['mobile']=$item['mobile']?:$item['member']['mobile'];
	$item['wechat']=$item['realname']?:$item['member']['wechat'];
	$return['item']=$item;	
	$htmls=array(
		'type'=>'look',
		'desc'=>'分销商其他信息',
	);
	$htmls['look']=array(
			array('type'=>'image','title'=>'头像','name'=>'member.avatar'),
			array('type'=>'string','title'=>'昵称','name'=>'member.nickname'),
			array('type'=>'string','title'=>'真实姓名','name'=>'realname'),
			array('type'=>'string','title'=>'电话','name'=>'mobile'),
			array('type'=>'string','title'=>'微信号','name'=>'wechat'),
			array(
						'title'=>'其他信息',
						'type'=>'array',
						'name'=>'diyformfields',
						'list'=>array(
							array('type'=>'string','name'=>'title','title'=>'表单标题'),	
							array('type'=>'labelarray','name'=>'data','title'=>'表单内容'),	
						),
			)
	);
	$return['htmls']=$htmls;
	exi($return);
}
?>